﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using APP.HELPER;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;

    public class xucListContractEmployee : UserControl
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiViewByDepartment;
        private BarButtonItem bbiViewByEmployee;
        private SimpleButton btMenu;
        private ButtonEdit btSearch;
        private IContainer components = null;
        private DataTable dt_Employee;
        private DevExpress.Utils.ImageCollection imgList;
        private ImageListBoxControl imgListEmployee;
        private Organization l_Organization;
        private string m_Code = MyLogin.Code;
        private string m_EmployeeCode = "";
        private int m_Level = MyLogin.Level;
        private SelectionMode m_ListSelectionMode = SelectionMode.One;
        private int m_Status = -1;
        private PanelControl panelControl1;
        private PopupMenu popupMenu1;
        private xucOrganizationEdit xucOrganizationEdit1;

        public event DepartmentSelectedHandler DepartmentSelected;

        public event EmployeeSelectedHandler EmployeeSelected;

        public xucListContractEmployee()
        {
            this.InitializeComponent();
        }

        private void bbiViewByDepartment_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.xucOrganizationEdit1.BringToFront();
            this.imgListEmployee.Enabled = false;
            this.imgListEmployee.BackColor = Color.WhiteSmoke;
            this.imgListEmployee.ForeColor = Color.DimGray;
            if (this.l_Organization == null)
            {
                this.l_Organization = this.xucOrganizationEdit1.Organization;
            }
            this.RaiseDepartmentSelectedHandler(this.l_Organization);
        }

        private void bbiViewByEmployee_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.btSearch.BringToFront();
            this.imgListEmployee.Enabled = true;
            this.imgListEmployee.BackColor = Color.White;
            this.imgListEmployee.ForeColor = Color.Black;
            if (this.m_EmployeeCode != "")
            {
                this.RaiseEmployeeSelectedHandler(this.m_EmployeeCode);
            }
        }

        private void btMenu_Click(object sender, EventArgs e)
        {
            this.popupMenu1.ShowPopup(Control.MousePosition);
        }

        private void btSearch_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            this.Search(this.dt_Employee, this.btSearch.Text);
        }

        private void btSearch_EditValueChanged(object sender, EventArgs e)
        {
            this.Search(this.dt_Employee, this.btSearch.Text);
            if (this.btSearch.Text == "")
            {
                this.btSearch.EditValue = null;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private string GetCode(ImageListBoxItem item)
        {
            try
            {
                string str = item.Value.ToString();
                char[] separator = new char[] { '(', ')' };
                return str.Split(separator)[1];
            }
            catch
            {
                return "";
            }
        }

        private void imgListEmployee_KeyDown(object sender, KeyEventArgs e)
        {
            this.imgListEmployee.SelectionMode = SelectionMode.MultiSimple;
            if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.A))
            {
                this.imgListEmployee.SelectAll();
            }
        }

        private void imgListEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.RaiseEmployeeSelectedHandler(this.m_EmployeeCode = this.GetCode(this.imgListEmployee.SelectedItem as ImageListBoxItem));
            }
            catch
            {
            }
            if (this.m_ListSelectionMode == SelectionMode.One)
            {
                if (Control.ModifierKeys != Keys.None)
                {
                    this.imgListEmployee.SelectionMode = SelectionMode.MultiSimple;
                }
                else
                {
                    this.imgListEmployee.SelectionMode = SelectionMode.One;
                }
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucListContractEmployee));
            SerializableAppearanceObject appearance = new SerializableAppearanceObject();
            MenuButton button = new MenuButton();
            ItemCommand command = new ItemCommand();
            ItemCommand command2 = new ItemCommand();
            ItemCommand command3 = new ItemCommand();
            ItemCommand command4 = new ItemCommand();
            ItemCommand command5 = new ItemCommand();
            ItemCommand command6 = new ItemCommand();
            ItemCommand command7 = new ItemCommand();
            ItemCommand command8 = new ItemCommand();
            ItemCommand command9 = new ItemCommand();
            ItemCommand command10 = new ItemCommand();
            ItemCommand command11 = new ItemCommand();
            ItemCommand command12 = new ItemCommand();
            ItemCommand command13 = new ItemCommand();
            ItemCommand command14 = new ItemCommand();
            ItemCommand command15 = new ItemCommand();
            ItemCommand command16 = new ItemCommand();
            ItemCommand command17 = new ItemCommand();
            ItemCommand command18 = new ItemCommand();
            ItemCommand command19 = new ItemCommand();
            ItemCommand command20 = new ItemCommand();
            ItemCommand command21 = new ItemCommand();
            ItemCommand command22 = new ItemCommand();
            ItemCommand command23 = new ItemCommand();
            ItemCommand command24 = new ItemCommand();
            ItemCommand command25 = new ItemCommand();
            ItemCommand command26 = new ItemCommand();
            ItemCommand command27 = new ItemCommand();
            ItemCommand command28 = new ItemCommand();
            ItemCommand command29 = new ItemCommand();
            this.imgListEmployee = new ImageListBoxControl();
            this.imgList = new DevExpress.Utils.ImageCollection(this.components);
            this.panelControl1 = new PanelControl();
            this.btMenu = new SimpleButton();
            this.btSearch = new ButtonEdit();
            this.xucOrganizationEdit1 = new xucOrganizationEdit();
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.bbiViewByEmployee = new BarButtonItem();
            this.bbiViewByDepartment = new BarButtonItem();
            this.popupMenu1 = new PopupMenu(this.components);
            ((ISupportInitialize) this.imgListEmployee).BeginInit();
            this.imgList.BeginInit();
            this.panelControl1.BeginInit();
            this.panelControl1.SuspendLayout();
            this.btSearch.Properties.BeginInit();
            this.barManager1.BeginInit();
            this.popupMenu1.BeginInit();
            base.SuspendLayout();
            this.imgListEmployee.Dock = DockStyle.Fill;
            this.imgListEmployee.HighlightedItemStyle = HighlightStyle.Skinned;
            this.imgListEmployee.HotTrackItems = true;
            this.imgListEmployee.HotTrackSelectMode = HotTrackSelectMode.SelectItemOnClick;
            this.imgListEmployee.ImageList = this.imgList;
            this.imgListEmployee.Location = new Point(0, 0x2e);
            this.imgListEmployee.Name = "imgListEmployee";
            this.imgListEmployee.Size = new Size(0x115, 430);
            this.imgListEmployee.TabIndex = 0;
            this.imgListEmployee.SelectedIndexChanged += new EventHandler(this.imgListEmployee_SelectedIndexChanged);
            this.imgListEmployee.KeyDown += new KeyEventHandler(this.imgListEmployee_KeyDown);
           // this.imgList.ImageStream = (ImageCollectionStreamer) manager.GetObject("imgList.ImageStream");
            this.imgList.Images.SetKeyName(0, "male.png");
            this.imgList.Images.SetKeyName(1, "user_woman.png");
            this.panelControl1.Controls.Add(this.btMenu);
            this.panelControl1.Controls.Add(this.btSearch);
//            this.panelControl1.Controls.Add(this.xucOrganizationEdit1);
            this.panelControl1.Dock = DockStyle.Top;
            this.panelControl1.Location = new Point(0, 0);
            this.panelControl1.Name = "panelControl1";
            this.panelControl1.Size = new Size(0x115, 0x2e);
            this.panelControl1.TabIndex = 1;
            this.btMenu.Appearance.Font = new Font("Lao UI", 8.25f);
            this.btMenu.Appearance.Options.UseFont = true;
            this.btMenu.Location = new Point(5, 10);
            this.btMenu.Name = "btMenu";
            this.btMenu.Size = new Size(0x1a, 0x17);
            this.btMenu.TabIndex = 1;
            this.btMenu.Text = "▼";
            this.btMenu.Click += new EventHandler(this.btMenu_Click);
            this.btSearch.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.btSearch.Location = new Point(0x23, 12);
            this.btSearch.Name = "btSearch";
            this.btSearch.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.btSearch.Properties.Appearance.Options.UseFont = true;
           // this.btSearch.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Glyph, "T\x00ecm", -1, true, true, false, ImageLocation.MiddleCenter, (Image) manager.GetObject("btSearch.Properties.Buttons"), new KeyShortcut(Keys.None), appearance, "", null, null, true) });
            this.btSearch.Properties.NullText = "[Lọc nh\x00e2n vi\x00ean theo từ kh\x00f3a]";
            this.btSearch.Size = new Size(0xed, 0x16);
            this.btSearch.TabIndex = 0;
            this.btSearch.ButtonClick += new ButtonPressedEventHandler(this.btSearch_ButtonClick);
            this.btSearch.EditValueChanged += new EventHandler(this.btSearch_EditValueChanged);
            this.xucOrganizationEdit1.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.xucOrganizationEdit1.BranchCode = null;
            this.xucOrganizationEdit1.Code = "";
            this.xucOrganizationEdit1.DepartmentCode = null;
            this.xucOrganizationEdit1.EmployeeCode = null;
            this.xucOrganizationEdit1.GroupCode = null;
            this.xucOrganizationEdit1.IsClose = CloseOrNew.None;
            this.xucOrganizationEdit1.Level = 0;
            this.xucOrganizationEdit1.Location = new Point(0x23, 12);
            this.xucOrganizationEdit1.Name = "xucOrganizationEdit1";
            this.xucOrganizationEdit1.NotSave = false;
            command.Caption = "";
            command.Enable = false;
            command.Visibility = BarItemVisibility.Never;
            button.Add = command;
            command2.Caption = "";
            command2.Enable = false;
            command2.Visibility = BarItemVisibility.Never;
            button.Cancel = command2;
            command3.Caption = "";
            command3.Enable = false;
            command3.Visibility = BarItemVisibility.Never;
            button.Change = command3;
            command4.Caption = "";
            command4.Enable = false;
            command4.Visibility = BarItemVisibility.Never;
            button.Close = command4;
            command5.Caption = "";
            command5.Enable = false;
            command5.Visibility = BarItemVisibility.Never;
            button.Custom = command5;
            command6.Caption = "";
            command6.Enable = false;
            command6.Visibility = BarItemVisibility.Never;
            button.Delete = command6;
            command7.Caption = "";
            command7.Enable = false;
            command7.Visibility = BarItemVisibility.Never;
            button.Export = command7;
            command8.Caption = "";
            command8.Enable = false;
            command8.Visibility = BarItemVisibility.Never;
            button.Filter = command8;
            command9.Caption = "";
            command9.Enable = false;
            command9.Visibility = BarItemVisibility.Never;
            button.FilterCustomer = command9;
            command10.Caption = "";
            command10.Enable = false;
            command10.Visibility = BarItemVisibility.Never;
            button.FilterStock = command10;
            command11.Caption = "";
            command11.Enable = false;
            command11.Visibility = BarItemVisibility.Never;
            button.Find = command11;
            button.GClose = false;
            button.GCommand = false;
            button.GExport = false;
            command12.Caption = "";
            command12.Enable = false;
            command12.Visibility = BarItemVisibility.Never;
            button.Go = command12;
            button.GOption = false;
            button.GPrint = false;
            button.GRecords = false;
            button.GSearchBar = false;
            button.GSettings = false;
            button.GVaildation = false;
            command13.Caption = "";
            command13.Enable = false;
            command13.Visibility = BarItemVisibility.Never;
            button.Import = command13;
            command14.Caption = "";
            command14.Enable = false;
            command14.Visibility = BarItemVisibility.Never;
            button.Next = command14;
            command15.Caption = "";
            command15.Enable = false;
            command15.Visibility = BarItemVisibility.Never;
            button.PageSetup = command15;
            button.PHome = false;
            command16.Caption = "";
            command16.Enable = false;
            command16.Visibility = BarItemVisibility.Never;
            button.Previous = command16;
            command17.Caption = "";
            command17.Enable = false;
            command17.Visibility = BarItemVisibility.Never;
            button.Print = command17;
            command18.Caption = "";
            command18.Enable = false;
            command18.Visibility = BarItemVisibility.Never;
            button.PrintPreview = command18;
            button.PTool = false;
            command19.Caption = "";
            command19.Enable = false;
            command19.Visibility = BarItemVisibility.Never;
            button.Redo = command19;
            command20.Caption = "";
            command20.Enable = false;
            command20.Visibility = BarItemVisibility.Never;
            button.Refresh = command20;
            command21.Caption = "";
            command21.Enable = false;
            command21.Visibility = BarItemVisibility.Never;
            button.Save = command21;
            command22.Caption = "";
            command22.Enable = false;
            command22.Visibility = BarItemVisibility.Never;
            button.SaveAndClose = command22;
            command23.Caption = "";
            command23.Enable = false;
            command23.Visibility = BarItemVisibility.Never;
            button.SaveAndNew = command23;
            command24.Caption = "";
            command24.Enable = false;
            command24.Visibility = BarItemVisibility.Never;
            button.Search = command24;
            command25.Caption = "";
            command25.Enable = false;
            command25.Visibility = BarItemVisibility.Never;
            button.SearchText = command25;
            command26.Caption = "";
            command26.Enable = false;
            command26.Visibility = BarItemVisibility.Never;
            button.Task = command26;
            command27.Caption = "";
            command27.Enable = false;
            command27.Visibility = BarItemVisibility.Never;
            button.Undo = command27;
            command28.Caption = "";
            command28.Enable = false;
            command28.Visibility = BarItemVisibility.Never;
            button.Vaildate = command28;
            command29.Caption = "";
            command29.Enable = false;
            command29.Visibility = BarItemVisibility.Never;
            button.View = command29;
            this.xucOrganizationEdit1.RibbonBar = button;
            this.xucOrganizationEdit1.Size = new Size(0xed, 0x16);
            this.xucOrganizationEdit1.Status = Actions.None;
            this.xucOrganizationEdit1.TabIndex = 8;
            this.xucOrganizationEdit1.Title = "";
            this.xucOrganizationEdit1.DepartmentSelected += new xucOrganizationEdit.DepartmentSelectedHandler(this.xucOrganizationEdit1_DepartmentSelected);
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiViewByEmployee, this.bbiViewByDepartment });
            this.barManager1.MaxItemId = 2;
            this.bbiViewByEmployee.Caption = "Xem theo nh\x00e2n vi\x00ean";
            this.bbiViewByEmployee.Id = 0;
            this.bbiViewByEmployee.Name = "bbiViewByEmployee";
            this.bbiViewByEmployee.ItemClick += new ItemClickEventHandler(this.bbiViewByEmployee_ItemClick);
            this.bbiViewByDepartment.Caption = "Xem theo ph\x00f2ng ban";
            this.bbiViewByDepartment.Id = 1;
            this.bbiViewByDepartment.Name = "bbiViewByDepartment";
            this.bbiViewByDepartment.ItemClick += new ItemClickEventHandler(this.bbiViewByDepartment_ItemClick);
            this.popupMenu1.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiViewByEmployee), new LinkPersistInfo(this.bbiViewByDepartment) });
            this.popupMenu1.Manager = this.barManager1;
            this.popupMenu1.Name = "popupMenu1";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
//            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.imgListEmployee);
            base.Controls.Add(this.panelControl1);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucListContractEmployee";
            base.Size = new Size(0x115, 0x1dc);
            ((ISupportInitialize) this.imgListEmployee).EndInit();
            this.imgList.EndInit();
            this.panelControl1.EndInit();
            this.panelControl1.ResumeLayout(false);
            this.btSearch.Properties.EndInit();
            this.barManager1.EndInit();
            this.popupMenu1.EndInit();
            base.ResumeLayout(false);
        }

        public void ListSelectionMode(SelectionMode mode)
        {
            this.imgListEmployee.SelectionMode = this.m_ListSelectionMode = mode;
        }

        public void LoadData(int Type)
        {
            this.xucOrganizationEdit1.LoadData();
            if (Type == 0)
            {
                this.bbiViewByEmployee_ItemClick(this, null);
            }
            else
            {
                this.bbiViewByDepartment_ItemClick(this, null);
            }
            this.LoadData(-1, MyLogin.Level, MyLogin.Code);
        }

        public void LoadData(int Status, int Level, string Code)
        {
            this.m_Status = Status;
            this.m_Level = Level;
            this.m_Code = Code;
            this.dt_Employee = new HRM_EMPLOYEE().GetListCurrentNow(this.m_Level, this.m_Code, this.m_Status);
            this.imgListEmployee.Items.Clear();
            foreach (DataRow row in this.dt_Employee.Rows)
            {
                ImageListBoxItem item = new ImageListBoxItem();
                if ((bool) row["Sex"])
                {
                    item.ImageIndex = 0;
                }
                else
                {
                    item.ImageIndex = 1;
                }
                item.Value = "(" + row["EmployeeCode"].ToString() + ") - " + row["FirstName"].ToString() + " " + row["LastName"].ToString();
                this.imgListEmployee.Items.Add(item);
            }
        }

        private void RaiseDepartmentSelectedHandler(Organization Item)
        {
            if (this.DepartmentSelected != null)
            {
                this.DepartmentSelected(this, Item);
            }
        }

        private void RaiseEmployeeSelectedHandler(string EmployeeCode)
        {
            if (this.EmployeeSelected != null)
            {
                this.EmployeeSelected(this, EmployeeCode);
            }
        }

        public void Search(DataTable Employees, string Value)
        {
            this.imgListEmployee.Items.Clear();
            foreach (DataRow row in Employees.Rows)
            {
                if ((row["EmployeeCode"].ToString().ToLower().Contains(Value.ToLower()) || row["FirstName"].ToString().ToLower().Contains(Value.ToLower())) || row["LastName"].ToString().ToLower().Contains(Value.ToLower()))
                {
                    ImageListBoxItem item = new ImageListBoxItem();
                    if ((bool) row["Sex"])
                    {
                        item.ImageIndex = 0;
                    }
                    else
                    {
                        item.ImageIndex = 1;
                    }
                    item.Value = "(" + row["EmployeeCode"].ToString() + ") - " + row["FirstName"].ToString() + " " + row["LastName"].ToString();
                    this.imgListEmployee.Items.Add(item);
                }
            }
        }

        private void xucOrganizationEdit1_DepartmentSelected(object sender, Organization Item)
        {
            this.LoadData(-1, Item.Level, Item.Code);
            this.RaiseDepartmentSelectedHandler(this.l_Organization = Item);
        }

        public delegate void DepartmentSelectedHandler(object sender, Organization Item);

        public delegate void EmployeeSelectedHandler(object sender, string EmployeeCode);
    }
}

